package com.mss.media.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @date 2020/12/21 21:46
 * @atuther crq
 */
public class JwtUtil {

    /**
     * 密钥
     */
    public static final String SECRET = "shelianbang;chenrunqiang";
    /**
     * 过期时间: S
     */
    public static final int EXPIRE = 60 * 60 * 1000;
    /**
     * 生成Token
     * @param adminAccount 账号
     * @return
     * @throws Exception
     */
    public static String createToken(String adminAccount, String adminName, Integer adminRole){
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.SECOND, EXPIRE);
        Date expireDate = nowTime.getTime();

        Map<String, Object> map = new HashMap<>();
        map.put("alg", "HS256");
        map.put("typ", "JWT");

        String token = JWT.create()
                .withHeader(map)//头
                .withClaim("adminAccount", adminAccount)
                .withClaim("adminName", adminName)
                .withClaim("adminRole", adminRole)
                .withSubject("成功")
                .withIssuedAt(new Date())//签名时间
                .withExpiresAt(expireDate)//过期时间
                .sign(Algorithm.HMAC256(SECRET));//签名
        return token;
    }

    /**
     * 验证Token
     * @param token
     * @return
     * @throws Exception
     */
    public static Map<String, Claim> verifyToken(String token){
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
        DecodedJWT jwt = null;
        try {
            jwt = verifier.verify(token);
        }catch (Exception e){
            throw new RuntimeException("凭证已过期，请重新登录");
        }
        return jwt.getClaims();
    }

    /**
     * 检验token是否正确
     */
    public static boolean isVerify(String token){
        try {
            Algorithm algorithm = Algorithm.HMAC256(SECRET);
            JWTVerifier verifier = JWT.require(algorithm).build();
            verifier.verify(token);
            return true;
        } catch (Exception e){
            return false;
        }
    }

    /**
     * 解析Token
     * @param token
     * @return
     */
    public static Map<String, Claim> parseToken(String token){
        DecodedJWT decodedJWT = JWT.decode(token);
        return decodedJWT.getClaims();
    }

    /**
     * 获取用户账号
     *
     * @param token token
     * @return 用户账号
     */
    public static String getUserAccount(String token){
        DecodedJWT decodedJWT = JWT.decode(token);
        Map<String, Claim> claims = decodedJWT.getClaims();
        String userAccount = claims.get("adminAccount").asString();
        return userAccount;
    }

    /**
     * 获取用户角色
     *
     * @param token token
     * @return 用户账号
     */
    public static String getUserRole(String token){
        DecodedJWT decodedJWT = JWT.decode(token);
        Map<String, Claim> claims = decodedJWT.getClaims();
        String userRole = claims.get("adminRole").asString();
        return userRole;
    }

    /**
     *从token解析出过期时间
     */
    public static Date parseExpiresTime(String token){
        DecodedJWT jwt = JWT.decode(token);
        return  jwt.getExpiresAt();
    }
    /**
     * 返回设置的过期秒数
     * @return long 秒数
     */
    public static long getExpireTime(){
        return  EXPIRE/1000;
    }

}